// pages/active/active.js
const app = getApp()
const promisify = require("../../utils/promisify")

Page({

  /**
   * 页面的初始数据
   */
  data: {
    tags: [{
        name: "全部",
        id: 0
      },
      {
        name: "志愿",
        id: 1
      },
      {
        name: "学术",
        id: 2
      },
      {
        name: "娱乐",
        id: 3
      }
    ],
    activities: [],
    selectedTag: 0,
    selectedActivity: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this
    promisify(wx.request)({
      url: `https://www.lional.club:6001/activities.json`,
      method: 'GET',
      header: {
        'content-type': 'application/json' // 默认值
      }
    }).then(
      data => {
        that.setData({
          activities: data.data.reverse()
        })
        that.changeView()
      }
    )
  },
  changeView: function () {
    /**
     * 通过不同标签，显示不同活动
     */
    let that = this
    if (that.data.selectedTag === 0) {
      that.setData({
        selectedActivity: that.data.activities
      })
    } else {
      let temp = []
      that.data.activities.forEach(element => {
        if (element.tagId === that.data.selectedTag) {
          temp.push(element)
        }
      });
      that.setData({
        selectedActivity: temp
      })
    }
  },
  tagChange: function (e) {
    /**
     * 标签的点击事件，会调用changeView来改变界面
     */
    let that = this
    let tag = e.currentTarget.dataset.text
    that.setData({
      selectedTag: tag
    })
    that.changeView()
  }
})